<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  <link rel="shortcut icon" href="../../img/favicon.ico">
  <title>Getting Host List From Master Server - Forge Networking Remastered</title>
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>

  <link rel="stylesheet" href="../../css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../css/theme_extra.css" type="text/css" />
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
  
  <script>
    // Current page data
    var mkdocs_page_name = "Getting Host List From Master Server";
    var mkdocs_page_input_path = "MasterServer/getting-host-list.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="../../js/jquery-2.1.1.min.js" defer></script>
  <script src="../../js/modernizr-2.8.3.min.js" defer></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
      <div class="wy-side-nav-search">
        <a href="../.." class="icon icon-home"> Forge Networking Remastered</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
	<ul class="current">
	  
          
            <li class="toctree-l1">
		
    <span class="caption-text">Home</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../..">User Manual (Forge Networking Remastered)</a>
                </li>
                <li class="">
                    
    <a class="" href="../../authoritative-design/">Authoritative Design</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Getting Started</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../GettingStarted/getting-started/">Getting Started</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/basic-moving-cube-example/">Basic Moving Cube Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/basic-rpc-example/">Basic RPC Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/basic-instantiation-example/">Basic Instantiation Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../../GettingStarted/jump-start-guide/">Jump Start Guide</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Network Contract Wizard (NCW)</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../NetworkContractWizard/network-contract-wizard-ncw/">Network Contract Wizard (NCW)</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkContractWizard/extending-generated-classes/">Extending Generated Classes</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkContractWizard/name-collision-issues/">Name Collision Issues</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Network Object</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../NetworkObject/changing-ownership/">Changing Ownership</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkObject/destroying-the-network-object/">Destroying the Network Object</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetworkObject/fields/">Fields</a>
                </li>
                <li class="">
                    
    <span class="caption-text">Remote Procedure Calls (RPCs)</span>
    <ul class="subnav">
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/remote-procedure-calls/">Remote Procedure Calls</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/buffered-rpcs/">Buffered RPCs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/sending-rpc-to-a-single-player/">Sending RPC to a Single Player</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/rpcargs-and-rpcinfo-structs/">RpcArgs and RpcInfo Structs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/rpc-validation-by-server/">RPC Validation by Server</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/replacing-previous-buffered-rpcs/">Replacing Previous Buffered RPCs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../../NetworkObject/RemoteProcedureCalls/clearing-buffered-rpcs/">Clearing Buffered RPCs</a>
                </li>
    </ul>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Unity Integration</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../UnityIntegration/gameobject-execution-order/">GameObject Execution Order</a>
                </li>
                <li class="">
                    
    <a class="" href="../../UnityIntegration/network-instantiation/">Network Instantiation</a>
                </li>
                <li class="">
                    
    <a class="" href="../../UnityIntegration/threading-in-unity/">Threading in Unity</a>
                </li>
                <li class="">
                    
    <a class="" href="../../UnityIntegration/main-threading-rpcs/">Main Threading RPCs</a>
                </li>
                <li class="">
                    
    <a class="" href="../../UnityIntegration/running-unity-specific-code-on-the-main-thread/">Running Unity specific code on the main thread</a>
                </li>
                <li class="">
                    
    <a class="" href="../../UnityIntegration/network-start/">Unity Integration Network Start</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Basic Network Samples</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/server-hosting-code/">Server Hosting Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/client-connecting-code/">Client Connecting Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/server-disconnect-client-code/">Server Disconnect Client Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/ban-client-code/">Ban Client Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/send-binary-frame/">Send Binary Frame</a>
                </li>
                <li class="">
                    
    <a class="" href="../../BasicNetworkSamples/send-file/">Send File</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Scene Navigation</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../SceneNavigation/loading-scenes/">Loading Scenes</a>
                </li>
                <li class="">
                    
    <a class="" href="../../SceneNavigation/scene-events/">Scene Events</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">NetWorker</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../NetWorker/temporarily-blocking-connections/">Temporarily Blocking Connections</a>
                </li>
                <li class="">
                    
    <a class="" href="../../NetWorker/thread-safe-player-iteration/">Thread Safe Player Iteration</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Master Server</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../quick-start/">Master Server Quick Start</a>
                </li>
                <li class="">
                    
    <a class="" href="../connecting-to-master-server/">Connecting to Master Server</a>
                </li>
                <li class=" current">
                    
    <a class="current" href="./">Getting Host List From Master Server</a>
    <ul class="subnav">
            
    <li class="toctree-l3"><a href="#getting-host-list-from-master-server">Getting Host List From Master Server</a></li>
    
        <ul>
        
            <li><a class="toctree-l4" href="#request-stages">Request stages</a></li>
        
            <li><a class="toctree-l4" href="#refresh-complete-sample">Refresh Complete Sample</a></li>
        
        </ul>
    

    </ul>
                </li>
                <li class="">
                    
    <a class="" href="../windows-server-firewall-setup/">Windows Server Firewall Setup</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Web Server</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../WebServer/jumpstart/">Web Server Jumpstart</a>
                </li>
                <li class="">
                    
    <a class="" href="../../WebServer/extending-with-mvc/">Web Server Extending With MVC</a>
                </li>
                <li class="">
                    
    <a class="" href="../../WebServer/command-plugins/">Web Server Command Plugins</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Troubleshooting</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../Troubleshooting/troubleshooting/">Troubleshooting</a>
                </li>
                <li class="">
                    
    <a class="" href="../../Troubleshooting/throttling-network-internal/">Throttling Network Internal</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../connection-cycle-events/">Events</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../rewinding/">Rewinding</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../network-logging/">Debugging</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../working-with-multiple-sockets/">Working With Multiple Sockets</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../nat-hole-punching/">NAT Hole Punching</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../lan-discovery/">LAN Discovery</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../offline-mode/">Offline Mode</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../lobby/">Lobby System</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../adding-and-updating-docs/">Contributing</a>
	    </li>
          
        </ul>
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../..">Forge Networking Remastered</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../..">Docs</a> &raquo;</li>
    
      
        
          <li>Master Server &raquo;</li>
        
      
    
    <li>Getting Host List From Master Server</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h1 id="getting-host-list-from-master-server">Getting Host List From Master Server</h1>
<p><em>If you have not already, we would suggest reviewing the <a href="quick-start">Master Server Quick Start</a> documentation to get familiar with the Master Server and to setup the stand alone executable and then reviewing <a href="connecting-to-master-server">Connecting to Master Server</a> to learn how to register your game server on the master server.</em></p>
<h2 id="request-stages">Request stages</h2>
<p>There are a few stages when requesting the list of servers from the master server:</p>
<ol>
<li>Connect to Master Server</li>
<li>Create a JSONNode request</li>
<li>Send the request to the server</li>
<li>Get response from the server</li>
<li>Parse response from server</li>
<li>Implement your code</li>
</ol>
<p>(1) So the very first thing you have to do is to connect to the master server. We have created a helper class to get you doing this quickly:</p>
<pre><code class="csharp">// TODO:  This will be the address of the machine your master server is running on
string host = &quot;127.0.0.1&quot;;

ushort port = 15937;

// The Master Server communicates over TCP
TCPMasterClient client = new TCPMasterClient();

// Just call the connect method and you are ready to go
client.Connect(host, ort);
</code></pre>

<p>(2) Once your server is accepted (which you can use the <code>serverAccepted</code> event) you can create your request to the master server. The request is a JSONNode object that follows this structure:</p>
<pre><code>{
    get:
    {
        id: &quot;myGame&quot;,
        type: &quot;any&quot;,
        mode: &quot;all&quot;
    }
}
</code></pre>

<p>You can do this with code similar to the following:</p>
<pre><code class="csharp">// The overall game id to select from
string gameId = &quot;myGame&quot;;

// The game type to choose from, if &quot;any&quot; then all types will be returned
string gameType = &quot;any&quot;;

// The game mode to choose from, if &quot;all&quot; then all game modes will be returned
string gameMode = &quot;all&quot;;

// Create the get request with the desired filters
JSONNode sendData = JSONNode.Parse(&quot;{}&quot;);
JSONClass getData = new JSONClass();

// The id of the game to get
getData.Add(&quot;id&quot;, gameId);
getData.Add(&quot;type&quot;, gameType);
getData.Add(&quot;mode&quot;, gameMode);

sendData.Add(&quot;get&quot;, getData);
</code></pre>

<p>(3) Now that we have the json <code>sendData</code> we need to send it to the server. Since JSON can be a string representation, you can send it directly to the server as a <strong>Text</strong> frame in Forge:</p>
<pre><code class="csharp">// Send the request to the server
client.Send(Frame.Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true));
</code></pre>

<p>(4) If you listen to the <code>textMessageReceived</code> event on the <code>client</code> object you will be able to capture the response from the server. This response will contain any errors (if any) and most importantly, your game list!</p>
<p>(5) Now that you have the JSON response from the server, you will need to parse it into the <code>MasterServerResponse</code> object. Lets imagine that your json response variable is called <code>data</code>, then the code you will need to parse the response to this object is:</p>
<pre><code class="csharp">// Create a C# object for the response from the master server
MasterServerResponse response = new MasterServerResponse(data[&quot;hosts&quot;].AsArray);
</code></pre>

<p><strong>Note</strong>: If <code>data["hosts"]</code> is null, then there was an error.</p>
<p>(6) Now that you have the <code>MasterServerResponse</code> object, you are ready to start writing your code for the UI and any other behaviors you want to execute based on the server listing.</p>
<pre><code class="csharp">foreach (MasterServerResponse.Server server in response.serverResponse)
{
    // TODO:  Do something with the server information here!
}
</code></pre>

<h2 id="refresh-complete-sample">Refresh Complete Sample</h2>
<p>Below is a complete example of how to:</p>
<ul>
<li>Connect to master server</li>
<li>Request the games list from the master server</li>
<li>Process the list of games returned from the master server</li>
<li>Disconnect from the master server</li>
</ul>
<p>The below example can be used to refresh the current listings of hosts from the master server. Please be sure to read the comments within the code to better understand what is going on. It is also good to scroll up and reference the top of this page to see why and when things happen in this sample.</p>
<pre><code class="csharp">public void Refresh()
{
    // TODO:  Clear out any previously listed servers

    // The Master Server communicates over TCP
    TCPMasterClient client = new TCPMasterClient();

    // Once this client has been accepted by the master server it should sent it's get request
    client.serverAccepted += () =&gt;
    {
        try
        {
            // The overall game id to select from
            string gameId = &quot;myGame&quot;;

            // The game type to choose from, if &quot;any&quot; then all types will be returned
            string gameType = &quot;any&quot;;

            // The game mode to choose from, if &quot;all&quot; then all game modes will be returned
            string gameMode = &quot;all&quot;;

            // Create the get request with the desired filters
            JSONNode sendData = JSONNode.Parse(&quot;{}&quot;);
            JSONClass getData = new JSONClass();

            // The id of the game to get
            getData.Add(&quot;id&quot;, gameId);
            getData.Add(&quot;type&quot;, gameType);
            getData.Add(&quot;mode&quot;, gameMode);

            sendData.Add(&quot;get&quot;, getData);

            // Send the request to the server
            client.Send(Frame.Text.CreateFromString(client.Time.Timestep, sendData.ToString(), true, Receivers.Server, MessageGroupIds.MASTER_SERVER_GET, true));
        }
        catch
        {
            // If anything fails, then this client needs to be disconnected
            client.Disconnect(true);
            client = null;
        }
    };

    // An event that is raised when the server responds with hosts
    client.textMessageReceived += (player, frame) =&gt;
    {
        try
        {
            // Get the list of hosts to iterate through from the frame payload
            JSONNode data = JSONNode.Parse(frame.ToString());
            if (data[&quot;hosts&quot;] != null)
            {
                // Create a C# object for the response from the master server
                MasterServerResponse response = new MasterServerResponse(data[&quot;hosts&quot;].AsArray);

                if (response != null &amp;&amp; response.serverResponse.Count &gt; 0)
                {
                    // Go through all of the available hosts and add them to the server browser
                    foreach (MasterServerResponse.Server server in response.serverResponse)
                    {
                        Debug.Log(&quot;Name: &quot; + server.Name);
                        Debug.Log(&quot;Address: &quot; + server.Address);
                        Debug.Log(&quot;Port: &quot; + server.Port);
                        Debug.Log(&quot;Comment: &quot; + server.Comment);
                        Debug.Log(&quot;Type: &quot; + server.Type);
                        Debug.Log(&quot;Mode: &quot; + server.Mode);
                        Debug.Log(&quot;Players: &quot; + server.Players);
                        Debug.Log(&quot;Max Players: &quot; + server.MaxPlayers);
                        Debug.Log(&quot;Protocol: &quot; + server.Protocol);

                        // TODO:  Update UI or something with the above data
                    }
                }
            }
        }
        finally
        {
            if (client != null)
            {
                // If we succeed or fail the client needs to disconnect from the Master Server
                client.Disconnect(true);
                client = null;
            }
        }
    };

    client.Connect(masterServerHost, (ushort)masterServerPort);
}
</code></pre>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../windows-server-firewall-setup/" class="btn btn-neutral float-right" title="Windows Server Firewall Setup">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../connecting-to-master-server/" class="btn btn-neutral" title="Connecting to Master Server"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" style="cursor: pointer">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="../connecting-to-master-server/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="../windows-server-firewall-setup/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js" defer></script>
      <script src="../../search/main.js" defer></script>

</body>
</html>
